package com.pls.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import com.pls.entity.EmployeeSeat;

public class ProgramHelper {

	public int executeIntParseReader(BufferedReader reader,
			String initialMessage, String errorMessage, int lengthValidation)
			throws IOException {
		int result = 0;
		boolean isSuccess = false;
		boolean error = false;

		while (!isSuccess) {
			try {
				if (error) {
					System.out.print(errorMessage);
				} else {
					System.out.print(initialMessage);
				}
				reader = new BufferedReader(new InputStreamReader(System.in));
				result = Integer.parseInt(reader.readLine());

				if (lengthValidation != 0) {
					if (Integer.toString(result).length() != lengthValidation) {
						throw new NumberFormatException();
					}
				}

			} catch (NumberFormatException e) {
				error = true;
				continue;
			} catch (Exception e) {
				error = true;
				continue;
			}
			isSuccess = true;
		}
		return result;
	}

	public int executeIntRangeParseReader(BufferedReader reader,
			String initialMessage, String errorMessage, int lengthValidation)
			throws IOException {
		int result = 0;
		boolean isSuccess = false;
		boolean error = false;

		while (!isSuccess) {
			try {
				if (error) {
					System.out.print(errorMessage);
				} else {
					System.out.print(initialMessage);
				}
				reader = new BufferedReader(new InputStreamReader(System.in));
				result = Integer.parseInt(reader.readLine());

				if (lengthValidation != 0) {
					if (result > lengthValidation) {
						error = true;
						continue;
					}
				}

			} catch (NumberFormatException e) {
				error = true;
				continue;
			} catch (Exception e) {
				error = true;
				continue;
			}
			isSuccess = true;
		}
		return result;
	}

	public int executeIntParseReader(BufferedReader reader,
			String initialMessage, String errorMessage) throws IOException {
		int result = 0;
		boolean isSuccess = false;
		boolean error = false;

		while (!isSuccess) {
			try {
				if (error) {
					System.out.print(errorMessage);
				} else {
					System.out.print(initialMessage);
				}
				reader = new BufferedReader(new InputStreamReader(System.in));
				result = Integer.parseInt(reader.readLine());

			} catch (NumberFormatException e) {
				error = true;
				continue;
			} catch (Exception e) {
				error = true;
				continue;
			}
			isSuccess = true;
		}

		return result;
	}

	public String executeStringReader(BufferedReader reader,
			String successMessage, String errorMessage) throws IOException {
		String result = "";
		boolean isSuccess = false;
		boolean error = false;

		while (!isSuccess) {
			try {
				if (!error) {
					System.out.print(successMessage);
				}
				reader = new BufferedReader(new InputStreamReader(System.in));
				result = reader.readLine();

			} catch (Exception e) {
				error = true;
				continue;
			}

			isSuccess = true;
		}
		return result;
	}

	public String executeStringAnswerReader(BufferedReader reader,
			String successMessage, String errorMessage) throws IOException {
		String result = "";
		boolean isSuccess = false;
		boolean error = false;

		while (!isSuccess) {
			try {
				if (error) {
					System.out.print(errorMessage);
				}

				else {
					System.out.print(successMessage);
				}
				reader = new BufferedReader(new InputStreamReader(System.in));
				result = reader.readLine();
				if (!result.equals("y") && !result.equals("n")) {
					error = true;
					continue;
				}

			} catch (Exception e) {
				error = true;
				continue;
			}

			isSuccess = true;
		}
		return result;
	}

	public void populateStringBuilder(List<EmployeeSeat> quadrant,
			EmployeeSeat employeeSeatDetails, StringBuilder sbSeatCode,
			StringBuilder sbEmpId, StringBuilder sbLocalNo, int row) {

		int space = Configuration.seatSpace;
		int empSpace;
		int seatSpace;
		int localSpace;
		int index = 0;

		sbSeatCode = new StringBuilder(" | ");
		sbEmpId = new StringBuilder(" | ");
		sbLocalNo = new StringBuilder(" | ");
		while (quadrant.get(index).getRow() == row) {

			String seatCode = quadrant.get(index).getSeatCode();
			String employeeCode = (quadrant.get(index).getEmployee().getId() == employeeSeatDetails
					.getEmployee().getId()) ? "*EMP"
					+ quadrant.get(index).getEmployee().getId() + "*" : "EMP"
					+ quadrant.get(index).getEmployee().getId();

			String localNo = (quadrant.get(index).getLocalNo() == null) ? ""
					: quadrant.get(index).getLocalNo();

			empSpace = space - employeeCode.length();
			seatSpace = space - seatCode.length();
			localSpace = space - localNo.length();

			// Append Seat Code in String Builder
			sbSeatCode.append(seatCode);
			for (int i = 0; i < seatSpace; i++) {
				sbSeatCode.append(" ");
			}
			sbSeatCode.append("| ");

			// Append Employee ID in String Builder
			sbEmpId.append(employeeCode);
			for (int i = 0; i < empSpace; i++) {
				sbEmpId.append(" ");
			}
			sbEmpId.append("| ");

			// Append Local Number in String Builder
			sbLocalNo.append(localNo);
			for (int i = 0; i < localSpace; i++) {
				sbLocalNo.append(" ");
			}
			sbLocalNo.append("| ");

			index++;

			if (quadrant.size() == index) {
				break;
			}

		}
	}
}
